﻿using Kenly.DBFramework.Gateway;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Collections;

namespace Kenly.DBFramework.Tests
{
    class Model : EntityValidator
    {
        public string ModelName
        {
            get;
            set;
        }

        public override bool Validate(out List<RuleViolation> violations)
        {
            violations = new List<RuleViolation>();
            if (string.IsNullOrEmpty(this.ModelName))
            {
                violations.Add(new RuleViolation("ModelName is empty"));
            }

            return violations.Count == 0;
        }
    }

    /// <summary>
    ///这是 EntityValidatorTest 的测试类，旨在
    ///包含所有 EntityValidatorTest 单元测试
    ///</summary>
    [TestClass()]
    public class EntityValidatorTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试属性
        // 
        //编写测试时，还可使用以下属性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///IsValid 的测试
        ///</summary>
        [TestMethod()]
        public void IsValidTest()
        {
            Model target = new Model();
            Assert.IsFalse(target.IsValid);

            target.ModelName = "ModelName";
            Assert.IsTrue(target.IsValid);
        }

        /// <summary>
        ///Validate 的测试
        ///</summary>
        [TestMethod()]
        public void ValidateTest()
        {
            Model target = new Model();
            Assert.IsFalse(target.IsValid);

            List<RuleViolation> violations;
            bool actual = target.Validate(out violations);
            Assert.IsFalse(actual);
            Assert.IsTrue(violations.Count == 1);

            target.ModelName = "ModelName";
            actual = target.Validate(out violations);
            Assert.IsTrue(target.IsValid);
            Assert.IsTrue(actual);
            Assert.IsTrue(violations.Count == 0);
        }

        /// <summary>
        ///FilterInvalid 的测试
        ///</summary>
        [TestMethod()]
        public void FilterInvalidTest()
        {
            IList entities = null;
            EntityValidator.FilterInvalid(entities);
            Assert.IsNull(entities);

            entities = new List<Model>();
            EntityValidator.FilterInvalid(entities);
            Assert.IsTrue(entities.Count == 0);

            entities.Add(new Model { ModelName = "ModelName" });
            entities.Add(new Model { ModelName = "" });
            EntityValidator.FilterInvalid(entities);
            Assert.IsTrue(entities.Count == 1);
        }

        /// <summary>
        ///FindInvalidIndexes 的测试
        ///</summary>
        [TestMethod()]
        public void FindInvalidIndexesTest()
        {
            IList<int> actual = EntityValidator.FindInvalidIndexes(null);
            Assert.IsTrue(actual.Count == 0);

            IList entities = new List<Model>();
            actual = EntityValidator.FindInvalidIndexes(entities);
            Assert.IsTrue(actual.Count == 0);

            entities.Add(new Model { ModelName = "ModelName" });
            entities.Add(new Model { ModelName = "" });
            actual = EntityValidator.FindInvalidIndexes(entities);
            Assert.IsTrue(actual.Count == 1);
            Assert.IsTrue(actual[0] == 1);
        }

        /// <summary>
        ///FindInvalid 的测试
        ///</summary>
        [TestMethod()]
        public void FindInvalidTest()
        {
            List<IEntityValidator> actual = EntityValidator.FindInvalid(null);
            Assert.IsTrue(actual.Count == 0);

            IList entities = new List<Model>();
            actual = EntityValidator.FindInvalid(entities);
            Assert.IsTrue(actual.Count == 0);

            entities.Add(new Model { ModelName = "ModelName" });
            entities.Add(new Model { ModelName = "" });
            actual = EntityValidator.FindInvalid(entities);
            Assert.IsTrue(actual.Count == 1);
        }
    }
}
